/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logic;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import persistence.Account;
import persistence.AuthenticationInfo;
import persistence.IdentityInfo;

/**
 *
 * @author asbriglio
 * On a pris le parti d'exposer le session bean au travers d'une interface métier locale
 */
@Stateful(name="AccountCreator")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AccountCreatorBean implements AccountCreatorLocal {

    private AuthenticationInfo authenticationInfo;
    private IdentityInfo identityInfo;

    @PersistenceContext(unitName="membersPU", type=PersistenceContextType.TRANSACTION)
    EntityManager em;//EM injecté. lié à un contexte de persistance transaction-scoped

    @Override
    public void addAuthenticationInfo(AuthenticationInfo authInfo) {
   authenticationInfo=authInfo;
    }

    @Override
    public void addIdentityInfo(IdentityInfo idenInfo) {
       identityInfo=idenInfo;
    }

    @Remove//l'invocation de cette méthode entraîne la destruction de l'instance.le dialogue est interrompu
    @Override
    public Account createAccount() {
       Account account = new Account();
       account.setAuthenticationInfo(authenticationInfo);
       account.setIdentityInfo(identityInfo);
       em.persist(account);

       return account;//détaché qd la méthode se termine car tsx "commitée"
    }
    @Remove
    @Override
    public void cancelAccount() {

        authenticationInfo=null;
        identityInfo=null;
    }
    
 
    
}
